import creep from "@/mount/creep"
import { generateID, isInArray } from "@/utils"

export default class WarMissonExtension extends Room {
    /* 战争相关任务 */
    // 攻防一体机
    public Task_AIO(misson:MissionModel):void{
        if (Game.time % 10) return
        if (this.controller.level < 7) return
        if (this.memory.StructureIdData.labs && this.memory.StructureIdData.labs.length < 4 ) return
        if (!misson.data.state) misson.data.state = 1
        if (!misson.data.startTime)misson.data.startTime = Game.time
        if (!this.Check_Lab(misson,'transport','complex')) return
        if (misson.data.state == 1)
        {
            /* 孵化爬虫 */
            if (misson.data.bodyNum) global.CreepNumData[this.name]['aio'].ability = misson.data.bodyNum
            global.CreepNumData[this.name]['aio'].num += misson.CreepBind['aio'].num
            // 跨shard作战
            if (misson.data.shard != Game.shard.name && misson.CreepBind['aio'].num == misson.CreepBind['aio'].bind.length)
            {
                misson.data.state = 2
            }
            // 同shard作战
            if (misson.data.shard == Game.shard.name)
            {
                var lessLifeNum = 0
                for (var i of misson.CreepBind['aio'].bind)
                {
                    if (Game.creeps[i] && Game.creeps[i].ticksToLive < 400)
                    lessLifeNum += 1
                }
                /* 提前补员 */
                global.CreepNumData[this.name]['aio'].num += lessLifeNum
                /* 任务绑定数据更新 */
                if (lessLifeNum > 0)
                    misson.CreepBind['aio'].num = misson.data.num + lessLifeNum
                else
                    misson.CreepBind['aio'].num = misson.data.num
                if (misson.CreepBind['aio'].num == misson.CreepBind['aio'].bind.length) misson.data.state = 2
            }
        }
        else if (misson.data.state == 2)
        {
            misson.CreepBind['aio'].num = 0
            if ((Game.time - misson.data.startTime) % misson.data.time == 0)
            {
                misson.CreepBind['aio'].num = misson.data.num
                misson.data.state = 1
            }
            if (this.memory.StructureIdData.labs && this.memory.StructureIdData.labs.length < 4 ) return

        }
    }


    // 双人小队
    public Task_Double(misson:MissionModel):void{
        if (!misson.data.time) misson.data.time = Game.time
        if ((Game.time - misson.data.time )% 10) return
        if (this.controller.level < 7) return
        if (!misson.data.state) misson.data.state = 1
        if (!misson.data.spaceTime)misson.data.spaceTime = 1300
        if (!this.Check_Lab(misson,'transport','storage')) return
        if (misson.data.state == 1)
        {
            /* 孵化爬虫 */
            if (!misson.data.teamType) return
            if (misson.data.teamType == 'dismantle')
            {
                global.CreepNumData[this.name]['double-dismantle'].num += misson.data.num
                global.CreepNumData[this.name]['double-heal'].num += misson.data.num
            }
            else
            {
                global.CreepNumData[this.name]['double-attack'].num += misson.data.num
                global.CreepNumData[this.name]['double-heal'].num += misson.data.num
            }
            // 跨shard作战
            var pdBoolean =misson.CreepBind['double-heal'].num != 0 && misson.CreepBind['double-heal'].num == misson.CreepBind['double-heal'].bind.length && (misson.CreepBind['double-attack']?misson.CreepBind['double-attack'].num == misson.CreepBind['double-attack'].bind.length : misson.CreepBind['double-dismantle'].num == misson.CreepBind['double-dismantle'].bind.length)

            if (misson.data.shard != Game.shard.name && pdBoolean)
            {
                misson.data.state = 2
                misson.CreepBind['double-heal'].num = 0
                if (misson.CreepBind['double-attack']) misson.CreepBind['double-attack'].num = 0
                else misson.CreepBind['double-dismantle'].num = 0
            }
        }
        else if (misson.data.state == 2)
        {
            misson.CreepBind['double-heal'].num = 0
            if (misson.CreepBind['double-attack']) misson.CreepBind['double-attack'].num = 0
            else misson.CreepBind['double-dismantle'].num = 0
            if ((Game.time - misson.data.time) % misson.data.spaceTime == 0)
            {
                misson.CreepBind['double-heal'].num =misson.data.num
                if (misson.CreepBind['double-attack']) misson.CreepBind['double-attack'].num = misson.data.num
                else misson.CreepBind['double-dismantle'].num = misson.data.num
                misson.data.state = 1
            }            
        }

    }


    /* 四人小队任务 ranged_attack类型  即将废弃*/
    public Task_X_CLASS_R(misson:MissionModel):void{
        if (Game.time % 5) return
        if (!misson.data.state) misson.data.state = 1
        if (!this.Check_Lab(misson,'transport','complex')) return
        if (misson.data.state == 1)
        {
            global.CreepNumData[this.name]['X-R-A'].num += 2
            global.CreepNumData[this.name]['X-R-B'].num += 2
            if (misson.CreepBind['X-R-A'].num == misson.CreepBind['X-R-A'].bind.length && misson.CreepBind['X-R-B'].num == misson.CreepBind['X-R-B'].bind.length)
            {
                misson.data.state = 2
            }
            
        }
        else if (misson.data.state == 2)
        {
            misson.CreepBind['X-R-A'].num = 0
            misson.CreepBind['X-R-B'].num = 0
            if ((Game.time - misson.data.startTime) % 800 == 0)
            {
                misson.CreepBind['X-R-A'].num = 2
                misson.CreepBind['X-R-B'].num = 2
                misson.data.state = 1
            }
        }
    }

    /* 四人小队通用任务孵化控制 */
    public Task_Squard(misson:MissionModel):void{
        // if (!misson.data.time) misson.data.time = Game.time
        if (Game.time % 5) return
        /* 填充t3 没填充好不会孵化 */
        if (!this.Check_Lab(misson,'transport','complex')) return

        
        // if (!misson.data.state) misson.data.state = 1
        // if (misson.data.state == 1)
        // {

        //     /* 生爬 */
        //     for (var i in misson.CreepBind)
        //     {
        //         /* 寻找shard中已经由框架控制的爬 */
        //         var num = 0
        //         if (Memory.squardMemory)
        //         {
        //             for (var i in Memory.squardMemory)
        //             {
        //                 /* 筛选同一个房间的任务 还需要是完整编队 */
        //                 if(Memory.squardMemory[i].sourceRoom == this.name && Memory.squardMemory[i].sourceShard == Game.shard.name && Memory.squardMemory[i].squardType == misson.data.sType && Object.keys(Memory.squardMemory[i].creepData).length == 4)
        //                 {
        //                     num += misson.CreepBind[i].num
        //                 }
        //             }
        //         }
        //         global.CreepNumData[this.name][i].num +=num +  misson.CreepBind[i].num
        //     }
            // if (Game.time != misson.data.time && (Game.time - misson.data.time)%misson.data.interval)
            // {

            // }
            /* 生满了就切换成待机模式，转由框架看控制后删除任务 */
            // var del = false
            // for (var i in misson.CreepBind)
            // {
            //     if (misson.CreepBind[i].bind.length != misson.CreepBind[i].num ) del = true
            // }
            // if (!del)
            // {
            // if (!misson.data.squardID) misson.data.squardID = `${Game.shard.name}/${Game.rooms}/${Game.time}`
            //     misson.data.state = 2
            // }
        // }
        // else if (misson.data.state == 2)
        // {
        //     /* 转由框架控制 */
        //     if (Memory.squardMemory[misson.data.squardID])
        //         this.DeleteMisson(misson.id)
        // }
    }

    /* 主动防御 */
    public Task_Auto_Defend():void{
        if (Game.time % 5) return
        if (!this.memory.roomSTATE) return
        if (this.memory.roomSTATE != 'war') return
        if (!Memory.ControlPlan[this.name] || !Memory.ControlPlan[this.name].defendPlan) return
        /* 激活主动防御 */
        var enemys = this.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
            return !isInArray(Memory.whitesheets,creep.owner.username) && (creep.owner.username != 'Invader')
        }})
        // console.log(enemys.length)
        if (this.MissonNum('Creep','主动防御') > 0)
        {
            /* 主动防御升级，暂缺 目前只孵化红球 */

            /* 孵化爬虫 */
            for (var m of this.memory.Misson['Creep'])
            {
                if(m.name == '主动防御')
                {
                    /* lab搬资源 */
                    this.Check_Lab(m,'transport','complex')
                    /* 孵化 */
                    for (var role in m.CreepBind)
                    {
                       // console.log(m.CreepBind[role].num)
                        global.CreepNumData[this.name][role].num += m.CreepBind[role].num
                        //console.log('globa;',global.CreepNumData[this.name][role].num)
                    }
                }
            }
            return
        }
        if (enemys.length <= 0) return
        var switch_ = false     // switch_ 为true是激活主动防御
        LoopA:
        for (var c of enemys)
        {
            for (var b of c.body)
            {
                if (b.boost && isInArray(['XGHO2','XKHO2','XUHO2','XZH2O','UH','UH2O'],b.boost))
                {
                    switch_ = true
                    break LoopA
                }
            }
        }
        console.log(switch_)
        if (!switch_) return
        /* 需要激活主动防御 */
        var thisTask = this.public_defend(1,0,0)
        if (!thisTask) return
        if (this.AddMisson(thisTask))
        Game.notify(`房间${this.name}激活主动防御！我们将抗战到底！`)
    }

    /* 黄球拆迁 */
    public Task_Dismantle(misson:MissionModel):void{
        if (Game.time % 10) return
        if (this.controller.level < 6) return
        if (this.memory.StructureIdData.labs && this.memory.StructureIdData.labs.length < 4 ) return
        if (!misson.data.state) misson.data.state = 1
        if (!misson.data.startTime)misson.data.startTime = Game.time
        if (!this.Check_Lab(misson,'transport','complex')) return
        if (misson.data.state == 1)
        {
            /* 孵化爬虫 */
            global.CreepNumData[this.name]['dismantle'].num += misson.CreepBind['dismantle'].num
            // 跨shard作战
            if (misson.data.shard != Game.shard.name && misson.CreepBind['dismantle'].num == misson.CreepBind['dismantle'].bind.length)
            {
                misson.data.state = 2
            }
        }
        else if (misson.data.state == 2)
        {
            misson.CreepBind['dismantle'].num = 0
            if ((Game.time - misson.data.startTime) % misson.data.time == 0)
            {
                misson.CreepBind['dismantle'].num = misson.data.num
                misson.data.state = 1
            }
        }
    }

    /* 控制攻击 */
    public Task_ClaimAttack(misson:MissionModel):void{
        if (Game.time % 10) return
        if (this.controller.level < 6) return
        if (!misson.data.state) misson.data.state = 1
        if (!misson.data.time) misson.data.time = 1500
        if (!misson.data.startTime)misson.data.startTime = Game.time
        if (misson.data.state == 1)
        {
            global.CreepNumData[this.name]['claim-attack'].num += misson.CreepBind['claim-attack'].num
            if (misson.data.shard != Game.shard.name && misson.CreepBind['claim-attack'].num == misson.CreepBind['claim-attack'].bind.length && misson.CreepBind['claim-attack'].num != 0)
            {
                misson.data.state = 2
            }
        }
        else if (misson.data.state == 2)
        {
            misson.CreepBind['claim-attack'].num = 0
            if ((Game.time - misson.data.startTime) % misson.data.time == 0)
            {
                misson.CreepBind['claim-attack'].num = misson.data.num
                misson.data.state = 1
            }
        }
    }

    /* om杀手 */
    public Task_OvermindKiller(misson:MissionModel):void{
        if (Game.time % 10) return
        if (!misson.data.state) misson.data.state = 1
        if (!misson.data.startTime)misson.data.startTime = Game.time
        if (misson.data.state == 1)
        {
            global.CreepNumData[this.name]['om-killer'].num += misson.CreepBind['om-killer'].num
            if (misson.data.shard != Game.shard.name && misson.CreepBind['om-killer'].num == misson.CreepBind['om-killer'].bind.length)
            {
                misson.data.state = 2
            }
        }
        else
        {
            misson.CreepBind['om-killer'].num = 0
            if ((Game.time - misson.data.startTime) % 800 == 0)
            {
                misson.CreepBind['om-killer'].num = 1
                misson.data.state = 1
            }
        }
    }
}